package com.ysh.sevlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ysh.domain.City;
import com.ysh.service.CityService;
import com.ysh.service.impl.CityServiceImpl;
import org.apache.ibatis.annotations.Select;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/city")
public class CityServlet extends HttpServlet {
    private CityService cityService = new CityServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("ajax请求...");
        List<City> list = cityService.findAll();
        // 存入作用域
        // req.setAttribute("list",list);
        // 跳转页面
        // req.getRequestDispatcher("/pages/add.jsp").forward(req,resp);

        // 将list集合转换成json格式数据，响应给客户端
        ObjectMapper obj = new ObjectMapper();
        String json = obj.writeValueAsString(list);
        // 向客户端响应数据
        resp.getWriter().write(json);

    }
}
